package cn.yitun.chat.yitunserverrunsingleboot.startup;

import cn.yitun.chat.yitunservercommonlib.db.MultiDataSource;
import cn.yitun.chat.yitunserverrunsingleboot.chat.properties.DbProperties;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源
 *
 * @Author: yitun
 * @Date: 2015年12月21日
 * @ModifyUser: yitun
 * @ModifyDate: 2015年12月21日
 */
@Configuration
//@PropertySource({"classpath:config/db.properties"})
//@PropertySource({"file:../../../../../../resources/config/db.properties"})
public class DataSourceConfig {

    @Resource
    private DbProperties dbProperties;

    // //////////////////////////////////////////写数据源
    @Bean(name = "writeDataSource")
    @Primary
    public DataSource writeDataSource() {
        MultiDataSource dataSource = new MultiDataSource();
        Map<Object, Object> dataSourcesMap = new HashMap<Object, Object>();
        try {
            DruidDataSource database = new DruidDataSource();
            database.setDriverClassName(dbProperties.getDriverClassName());
            database.setUrl(dbProperties.getMasterUrl());
            database.setUsername(dbProperties.getMasterUsername());
            database.setPassword(dbProperties.getMasterPassword());
            database.setFilters(dbProperties.getFilters());
            database.setMaxActive(dbProperties.getMaxActive());
            database.setInitialSize(dbProperties.getInitialSize());
            database.setMaxWait(dbProperties.getMaxWait());
            database.setMinIdle(dbProperties.getMinIdle());
            database.setTimeBetweenEvictionRunsMillis(dbProperties.getTimeBetweenEvictionRunsMillis());
            database.setMinEvictableIdleTimeMillis(dbProperties.getMinEvictableIdleTimeMillis());
            database.setValidationQuery(dbProperties.getValidationQuery());
            database.setTestWhileIdle(dbProperties.getTestWhileIdle());
            database.setTestOnBorrow(dbProperties.getTestOnBorrow());
            database.setTestOnReturn(dbProperties.getTestOnReturn());
            database.setPoolPreparedStatements(false);
            //database.setRemoveAbandoned(environment.getProperty("db.removeAbandoned", Boolean.class));
            //database.setRemoveAbandonedTimeout(environment.getProperty("db.removeAbandonedTimeout", Integer.class));
            database.setLogAbandoned(dbProperties.getLogAbandoned());
            dataSourcesMap.put("default", database);
            dataSource.setTargetDataSources(dataSourcesMap);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    // ///////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 读数据源
     *
     * @return
     */
    @Bean(name = "readDataSource")
    public DataSource readDataSource() {
        MultiDataSource dataSource = new MultiDataSource();
        Map<Object, Object> dataSourcesMap = new HashMap<Object, Object>();
        try {
            DruidDataSource database = new DruidDataSource();
            database.setDriverClassName(dbProperties.getDriverClassName());
            database.setUrl(dbProperties.getSlaveUrl());
            database.setUsername(dbProperties.getSlaveUsername());
            database.setPassword(dbProperties.getSlavePassword());
            database.setFilters(dbProperties.getFilters());
            database.setMaxActive(dbProperties.getMaxActive());
            database.setInitialSize(dbProperties.getInitialSize());
            database.setMaxWait(dbProperties.getMaxWait());
            database.setMinIdle(dbProperties.getMinIdle());
            database.setTimeBetweenEvictionRunsMillis(dbProperties.getTimeBetweenEvictionRunsMillis());
            database.setMinEvictableIdleTimeMillis(dbProperties.getMinEvictableIdleTimeMillis());
            database.setValidationQuery(dbProperties.getValidationQuery());
            database.setTestWhileIdle(dbProperties.getTestWhileIdle());
            database.setTestOnBorrow(dbProperties.getTestOnBorrow());
            database.setTestOnReturn(dbProperties.getTestOnReturn());
            database.setPoolPreparedStatements(false);
            //database.setRemoveAbandoned(environment.getProperty("db.removeAbandoned", Boolean.class));
            //database.setRemoveAbandonedTimeout(environment.getProperty("db.removeAbandonedTimeout", Integer.class));
            database.setLogAbandoned(dbProperties.getLogAbandoned());
            dataSourcesMap.put("default", database);
            dataSource.setTargetDataSources(dataSourcesMap);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
}
